package ch3.Singleton;

/**
 * 简单加锁实现的单例模式
 * 这一版中，使用了内部锁来做同步，但是这种加锁的开销有点大
 * 每一次执行getInstance()方法都要先申请锁
 */

public class SimpleMultithreadsSingleton {
    private static SimpleMultithreadsSingleton instance = null;

    private SimpleMultithreadsSingleton() {}

    // 创建并返回该类的唯一实例
    public static SimpleMultithreadsSingleton getInstance() {
        synchronized (SimpleMultithreadsSingleton.class) {
            if (null == instance) {
                instance = new SimpleMultithreadsSingleton();
            }
            return instance;
        }
    }
}
